package com.crazymaker.demo.rxJava.basic;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import rx.Observable;
import rx.Subscriber;
import rx.functions.Action1;

import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.TimeUnit;


@Slf4j
public class RxJavaObserverDemo
{


    /**
     * 演示  from 创建操作符的使用
     */
    @Test
    public void rxJavaFromTest()
    {
        formObserverDemo();
    }

    //from 创建操作符 ， 可将其他种类的对象和数据类型转换为 Observable。
    private void formObserverDemo()
    {
        List<String> fruitList = Arrays.asList("apple", "orange" );
        Observable.from(fruitList).subscribe(new Action1<String>()
        {
            @Override
            public void call(String fruit)
            {
                log.info("fruit = {}", fruit);
            }
        });
        log.info("--------  演示 链式调用 ---------" );
        Observable.from(fruitList)
                .subscribe(fruit -> log.info("fruit = {}", fruit));
    }


    /**
     * 演示  window 创建操作符   创建滑动窗口
     */
    @Test
    public void windowObserverDemoTest()
    {
        windowObserverDemo();
    }


    // window 创建操作符 创建滑动窗口
    private void windowObserverDemo()
    {
        List<Integer> list = Arrays.asList(10, 11, 20, 21, 30, 31);

        final int[] group = {0};
        Observable.from(list).window(2, 2).subscribe(new Action1<Observable<Integer>>()
        {
            @Override
            public void call(Observable<Integer> integerObservable)
            {
                final int[] index = {1};
                integerObservable.subscribe(new Action1<Integer>()
                {
                    @Override
                    public void call(Integer integer)
                    {
                        log.info(" 第{}组,第{}个, 值为： {}", group[0], index[0], integer);
                        index[0]++;
                    }
                });
                group[0]++;
            }
        });
    }


    /**
     * 演示  window 创建操作符   创建时间窗口
     */
    @Test
    public void timewindowObserverDemoTest()
    {
        timewindowObserverDemo();

        try
        {
            Thread.sleep(Integer.MAX_VALUE);
        } catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }


    // window 创建操作符 创建时间窗口
    private void timewindowObserverDemo()
    {
        Observable inputEventStream = Observable.create(new Observable.OnSubscribe<Object>()
        {
            @Override
            public void call(Subscriber<? super Object> subscriber)
            {
                subscriber.onNext(" producer call ........." );
            }
        });
        inputEventStream.window(1, TimeUnit.SECONDS).subscribe(new Action1()
        {
            @Override
            public void call(Object o)
            {
                Calendar calendar = Calendar.getInstance();
                int i = calendar.get(Calendar.SECOND);
                log.info("第{}秒唤醒触发...", i);
            }
        });

    }
}
